<?php
/**
 * Created by PhpStorm.
 * User: Bzing
 * Date: 2017/6/26
 * Time: 19:22
 */
namespace app\api\controller;
use app\models\Models;

class Follow extends Base {
    /**
     * 关注用户列表
     * @return \think\response\Json
     */
    public function followUserList(){
        if(request()->isPost()){
            try{
                // 签名验证,获取参数
                $params = self::paramsSign(['equipment','encrypt','user_id','page']);
                // 登录状态下
                if($params['token']){
                    // 登录验证
                    $params = self::checkLogin();
                }
                // 获取扫码有效用户
                $scan = self::isScan();
                $follow = new Models('follow');
                $join = [['user b','a.follow_id = b.id','left'],['location c','a.follow_id = c.uid','left']];
                $field = 'b.id,b.nickname,b.sex,b.birth,b.signature,b.wine,b.portrait,b.last_request_time,c.location';
                $where = ['a.fans_id'=>$params['user_id'],'a.follow_object'=>Models::FOLLOW_USER,'a.status'=>Models::FOLLOW_YES,'b.type_id'=>Models::USER_TYPE_NORMAL];
                $list = $follow->_page($where,$field,'a.create_time desc',$join,$params['page']);
                $list = $list->items();
                foreach($list as $k=>$v){
                    $list[$k]['background'] = '';
                    $list[$k]['name']       = '';
                    foreach($scan as $v1){
                        if($v['id'] == $v1['uid']){
                            $list[$k]['background'] = empty($v1['background']) ? '' : self::arrayMapBackground($v1['background']);
                            $list[$k]['name']       = $v1['name'];
                        }
                    }
                    $list[$k]['portrait']   = empty($v['portrait']) ? '' : self::arrayMapPortrait($v['portrait']);
                    $list[$k]['age']        = calcAge($v['birth']);
                    $list[$k]['last_request_time'] = formatDate($v['last_request_time']);
                    // 删除多余元素
                    unset($list[$k]['birth']);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 关注现场列表
     * @return \think\response\Json
     */
    public function followLiveList(){
        if(request()->isPost()){
            try{
                // 签名验证,获取参数
                $params = self::paramsSign(['equipment','encrypt','user_id','page']);
                // 登录状态下
                if($params['token']){
                    // 登录验证
                    $params = self::checkLogin();
                    // 查询用户关注商家的id
                    $follow = new Models('follow');
                    $list = $follow->_select(['fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_BUSINESS,'status'=>Models::FOLLOW_YES],'id');
                    $ids  = array_column($list,'id');
                }
                $follow = new Models('follow');
                $join  = [['business b','a.follow_id = b.id','left']];
                $field = 'b.id,b.name,b.background,b.fans_num';
                $where = ['a.fans_id'=>$params['user_id'],'a.follow_object'=>Models::FOLLOW_BUSINESS,'a.status'=>Models::FOLLOW_YES];
                $list = $follow->_page($where,$field,'a.create_time desc',$join,$params['page']);
                $list = $list->items();
                foreach($list as $k=>$v){
                    $list[$k]['background'] = empty($v['background']) ? '' : self::arrayMapBackground($v['background']);
                    $list[$k]['is_follow']  = empty($ids) ? Models::FOLLOW_NOT : (in_array($v['id'],$ids) ? Models::FOLLOW_YES : Models::FOLLOW_NOT);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 关注艺人列表
     * @return \think\response\Json
     */
    public function followActorList(){
        if(request()->isPost()){
            try{
                // 签名验证,获取参数
                $params = self::paramsSign(['equipment','encrypt','user_id','page']);
                // 登录状态下
                if($params['token']){
                    // 登录验证
                    $params = self::checkLogin();
                    // 查询用户关注艺人的id
                    $follow = new Models('follow');
                    $list = $follow->joinMany([['user b','a.follow_id = b.id','left']],['b.type_id'=>Models::USER_TYPE_ACTOR,'a.fans_id'=>$params['user_id'],'a.follow_object'=>Models::FOLLOW_USER,'a.status'=>Models::FOLLOW_YES],'b.id');
                    $ids  = array_column($list,'id');
                }
                $follow = new Models('follow');
                $join  = [['user b','a.follow_id = b.id','left']];
                $field = 'b.id,b.nickname,b.portrait,b.fans_num';
                $where = ['a.fans_id'=>$params['user_id'],'a.follow_object'=>Models::FOLLOW_USER,'a.status'=>Models::FOLLOW_YES,'b.type_id'=>Models::USER_TYPE_ACTOR];
                $list = $follow->_page($where,$field,'a.create_time desc',$join,$params['page']);
                $list = $list->items();
                foreach($list as $k=>$v){
                    $list[$k]['portrait'] = empty($v['portrait']) ? '' : self::arrayMapPortrait($v['portrait']);
                    $list[$k]['is_follow']  = empty($ids) ? Models::FOLLOW_NOT : (in_array($v['id'],$ids) ? Models::FOLLOW_YES : Models::FOLLOW_NOT);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 粉丝列表
     * @return \think\response\Json
     */
    public function fansList(){
        if(request()->isPost()){
            try{
                // 签名验证,获取参数
                $params = self::paramsSign(['equipment','encrypt','user_id','page']);
                // 登录状态下
                if($params['token']){
                    // 登录验证
                    $params = self::checkLogin();
                }
                // 获取扫码有效用户
                $scan = self::isScan();
                $follow = new Models('follow');
                $join = [['user b','a.fans_id = b.id','left'],['location c','a.fans_id = c.uid','left']];
                $field = 'b.id,b.nickname,b.sex,b.birth,b.signature,b.wine,b.portrait,b.last_request_time,c.location';
                $where = ['a.follow_id'=>$params['user_id'],'a.follow_object'=>Models::FOLLOW_USER,'a.status'=>Models::FOLLOW_YES];
                $list = $follow->_page($where,$field,'a.create_time desc',$join,$params['page']);
                $list = $list->items();
                foreach($list as $k=>$v){
                    $list[$k]['background'] = '';
                    $list[$k]['name']       = '';
                    foreach($scan as $v1){
                        if($v['id'] == $v1['uid']){
                            $list[$k]['background'] = empty($v1['background']) ? '' : self::arrayMapBackground($v1['background']);
                            $list[$k]['name']       = $v1['name'];
                        }
                    }
                    $list[$k]['portrait']   = empty($v['portrait']) ? '' : self::arrayMapPortrait($v['portrait']);
                    $list[$k]['age']        = calcAge($v['birth']);
                    $list[$k]['last_request_time'] = formatDate($v['last_request_time']);
                    // 删除多余元素
                    unset($list[$k]['birth']);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($list));
        }
    }

    /**
     * 关注/取消
     * @return \think\response\Json
     */
    public function follow(){
        if(request()->isPost()){
            $user = new Models('user');
            $follow = new Models('follow');
            try{
                // 登录验证
                $params = self::checkLogin(['equipment','encrypt','follow_id','follow_type']);
                $bool = $bool1 = $bool2 = true;
                if($params['follow_type'] == Models::FOLLOW_USER){
                    // 查询关注用户信息
                    $info = $follow->_find(['follow_id'=>$params['follow_id'],'fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_USER]);
                    if($info && $info['status'] == Models::FOLLOW_YES){
                        $bool  = $follow->_save(['id'=>$info['id'],'status'=>Models::FOLLOW_NOT]);
                        $bool1 = $user->_setDec(['id'=>$params['follow_id']],'fans_num');     // 粉丝数-1
                        $bool2 = $user->_setDec(['id'=>$params['uid']],'follow_user_num');    // 关注数-1
                        $status = ['status'=>Models::FOLLOW_NOT];
                    }
                    if($info && $info['status'] == Models::FOLLOW_NOT){
                        $bool = $follow->_save(['id'=>$info['id'],'status'=>Models::FOLLOW_YES,'create_time'=>time()]);
                        $bool1 = $user->_setInc(['id'=>$params['follow_id']],'fans_num');
                        $bool2 = $user->_setInc(['id'=>$params['uid']],'follow_user_num');
                        $status = ['status'=>Models::FOLLOW_YES];
                    }
                    if(!$info){
                        $bool = $follow->_add(['follow_id'=>$params['follow_id'],'fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_USER,'status'=>Models::FOLLOW_YES,'create_time'=>time()]);
                        $bool1 = $user->_setInc(['id'=>$params['follow_id']],'fans_num');
                        $bool2 = $user->_setInc(['id'=>$params['uid']],'follow_user_num');
                        $status = ['status'=>Models::FOLLOW_YES];
                    }
                }
                if($params['follow_type'] == Models::FOLLOW_BUSINESS){
                    // 查询关注商家信息
                    $business = new Models('business');
                    $info = $follow->_find(['follow_id'=>$params['follow_id'],'fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_BUSINESS]);
                    if($info && $info['status'] == Models::FOLLOW_YES){
                        $bool  = $follow->_save(['id'=>$info['id'],'status'=>Models::FOLLOW_NOT]);
                        $bool1 = $business->_setDec(['id'=>$params['follow_id']],'fans_num');     // 粉丝数-1
                        $bool2 = $user->_setDec(['id'=>$params['uid']],'follow_business_num');    // 关注数-1
                        $status = ['status'=>Models::FOLLOW_NOT];
                    }
                    if($info && $info['status'] == Models::FOLLOW_NOT){
                        $bool  = $follow->_save(['id'=>$info['id'],'status'=>Models::FOLLOW_YES,'create_time'=>time()]);
                        $bool1 = $business->_setInc(['id'=>$params['follow_id']],'fans_num');
                        $bool2 = $user->_setInc(['id'=>$params['uid']],'follow_business_num');
                        $status = ['status'=>Models::FOLLOW_YES];
                    }
                    if(!$info){
                        $bool  = $follow->_add(['follow_id'=>$params['follow_id'],'fans_id'=>$params['uid'],'follow_object'=>Models::FOLLOW_BUSINESS,'status'=>Models::FOLLOW_YES,'create_time'=>time()]);
                        $bool1 = $business->_setInc(['id'=>$params['follow_id']],'fans_num');
                        $bool2 = $user->_setInc(['id'=>$params['uid']],'follow_business_num');
                        $status = ['status'=>Models::FOLLOW_YES];
                    }
                }
                if(!$bool || !$bool1 || !$bool2){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return json(self::formatResult($e->getCode(),$e->getMessage()));
            }
            return json(self::formatSuccessResult($status));
        }
    }
}